﻿using System;
using System.Windows;
using Infrastructure.Controls;
using Infrastructure.Services;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using MVVMBasic.Interfaces;
using MVVMBasic.View;
using MVVMBasic.ViewModel;
using Infrastructure.Interfaces;
using MVVMBasicRIALibrary;


namespace MVVMBasic
{
    public class Bootstrapper : UnityBootstrapper
    {
        private readonly CallbackLogger callbackLogger = new CallbackLogger();

        /// <summary>
        /// Utworzenie widoku Shell
        /// ServiceLocator przejmuje funkcję Container.Resolve<IType>()
        /// opakowując kontener DI
        /// </summary>
        protected override DependencyObject CreateShell()
        {
            return ServiceLocator.Current.GetInstance<MainPage>();
        }

        /// <summary>
        /// Zainicjalizowanie głównego widoku
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.RootVisual = (UIElement)this.Shell;
        }

        /// <summary>
        /// Utworzenie własnego loggera 
        /// </summary>
        protected override ILoggerFacade CreateLogger()
        {
            return this.callbackLogger;
        }

        /// <summary>
        /// Konfiguracja kontenera DI: inicjalizacja Unity oraz mapowanie interfejs- typ np. IEventAggregator
        /// </summary>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            this.Container.RegisterInstance(this.callbackLogger);
            this.Container.RegisterType<ICustomerDetailsView, CustomerDetailsView>();
            this.Container.RegisterType<ICustomerDetailsViewViewModel, CustomerDetailsViewViewModel>();
            this.Container.RegisterType<IErrorWindow, ErrorWindow>();
            this.Container.RegisterType<IUnitOfWorkFactory, BasicUnitOfWorkFactory>();
            this.Container.RegisterType(typeof(IRepository<>), typeof(BasicRepository<>));
        }

        /// <summary>
        /// Utworzenie katalogu modułów na podstawie pliku XAML
        /// </summary>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            //var cat = new ModuleCatalog();
            //var rozrachunkiInfo = new ModuleInfo
            //{
            //    ModuleName = "HumanResourcesModule",
            //    ModuleType = "Vulcan.FIONET.Rozrachunki.FIONETRozrachunkiModule, FIONET.Rozrachunki, Version=1.0.0.0",
            //    Ref = "FIONET.Rozrachunki.xap",
            //    InitializationMode = InitializationMode.OnDemand,
            //};
            //cat.AddModule(rozrachunkiInfo);
            return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new 
                Uri("/MVVMBasic;component/ModulesCatalog.xaml", UriKind.Relative));
        }
    }
}
